package com.misalen.test;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.misalen.web.database.model.mysql.sys.SysAdmin;
import org.misalen.web.database.model.mysql.sys.SysAdminRole;
import org.misalen.web.database.model.mysql.sys.SysResources;
import org.misalen.web.database.model.mysql.sys.SysResources.SaveType;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.database.model.mysql.sys.SysRole;
import org.misalen.web.database.model.mysql.sys.SysRoleResources;
import org.misalen.web.server.controllers.BaseController;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/applicationContext.xml", "classpath:spring/spring-servlet.xml" })
public class 平台角色数据 extends BaseController {

	@Test
	public void Test() {
		sysAdminRoleRepository.delete(SysAdminRole.class, null);
		sysRoleResourcesRepository.delete(SysRoleResources.class, null);
		sysAdminRepository.delete(SysAdmin.class, null);
		sysRoleRepository.delete(SysRole.class, null);
		PaginationFrom<SysResources> paginationFrom = sysResourcesRepository.findPageByBean(new SysResources(), null);
		for (SysResources sysResources : paginationFrom.getList()) {
			if (sysResources.getSaveType().equals("button")) {
				sysResourcesRepository.delete(SysResources.class, sysResources.getPrimaryKey());
			}
		}
		for (SysResources sysResources : paginationFrom.getList()) {
			if (sysResources.getSaveType().equals("menu2")) {
				sysResourcesRepository.delete(SysResources.class, sysResources.getPrimaryKey());
			}
		}
		for (SysResources sysResources : paginationFrom.getList()) {
			if (sysResources.getSaveType().equals("menu1")) {
				sysResourcesRepository.delete(SysResources.class, sysResources.getPrimaryKey());
			}
		}

		SysAdmin admin = new SysAdmin();
		admin.setAddTime(new Date());
		admin.setLoginPwd("admin");
		admin.setNickname("超级管理员");
		admin.setUsername("admin");
		sysAdminRepository.save(admin);

		SysRole role_admin = new SysRole();
		role_admin.setAddTime(new Date());
		role_admin.setRoleDesc("拥有所有权限");
		role_admin.setCode("admin");
		role_admin.setRoleName("超级管理员");
		role_admin.setSeq(0);
		role_admin.setUsingState("available");
		sysRoleRepository.save(role_admin);

		SysRole role_national = new SysRole();
		role_national.setAddTime(new Date());
		role_national.setRoleDesc("全国代理");
		role_national.setCode("agent-national");
		role_national.setRoleName("全国代理");
		role_national.setSeq(1);
		role_national.setUsingState("available");
		sysRoleRepository.save(role_national);

		SysRole role_shanghai = new SysRole();
		role_shanghai.setAddTime(new Date());
		role_shanghai.setRoleDesc("上海代理");
		role_shanghai.setCode("agent-shanghai");
		role_shanghai.setRoleName("上海代理");
		role_shanghai.setSeq(2);
		role_shanghai.setUsingState("available");
		sysRoleRepository.save(role_shanghai);

		SysRole role_lanzhou = new SysRole();
		role_lanzhou.setAddTime(new Date());
		role_lanzhou.setRoleDesc("兰州代理");
		role_lanzhou.setCode("agent-lanzhou");
		role_lanzhou.setRoleName("兰州代理");
		role_lanzhou.setSeq(3);
		role_lanzhou.setUsingState("available");
		sysRoleRepository.save(role_lanzhou);

		SysResources resourcesAdmin = new SysResources();
		resourcesAdmin.setAddTime(new Date());
		resourcesAdmin.setIcon("icon-list-alt");
		resourcesAdmin.setResourceName("管理员中心");
		resourcesAdmin.setSaveType(SaveType.menu1.name());
		resourcesAdmin.setSeq(0);
		resourcesAdmin.setUsingState("available");
		sysResourcesRepository.save(resourcesAdmin);

		SysResources resourcesAdminList = new SysResources();
		resourcesAdminList.setAddTime(new Date());
		resourcesAdminList.setIcon("icon-list-alt");
		resourcesAdminList.setResourceName("管理员列表");
		resourcesAdminList.setSaveType(SaveType.menu2.name());
		resourcesAdminList.setSeq(0);
		resourcesAdminList.setResourceUrl("/sys-admin/");
		resourcesAdminList.setUsingState("available");
		resourcesAdminList.setParentId(resourcesAdmin.getPrimaryKey());
		sysResourcesRepository.save(resourcesAdminList);

		SysResources resourcesUser = new SysResources();
		resourcesUser.setAddTime(new Date());
		resourcesUser.setIcon("icon-list-alt");
		resourcesUser.setResourceName("普通用户中心");
		resourcesUser.setSaveType(SaveType.menu1.name());
		resourcesUser.setSeq(1);
		resourcesUser.setUsingState("available");
		sysResourcesRepository.save(resourcesUser);

		SysResources resourcesBanner = new SysResources();
		resourcesBanner.setAddTime(new Date());
		resourcesBanner.setIcon("icon-list-alt");
		resourcesBanner.setResourceName("Banner管理");
		resourcesBanner.setSaveType(SaveType.menu2.name());
		resourcesBanner.setSeq(1);
		resourcesBanner.setResourceUrl("/pro-user-banner/");
		resourcesBanner.setUsingState("available");
		resourcesBanner.setParentId(resourcesAdmin.getPrimaryKey());
		sysResourcesRepository.save(resourcesBanner);

		SysResources resourcesVersion = new SysResources();
		resourcesVersion.setAddTime(new Date());
		resourcesVersion.setIcon("icon-list-alt");
		resourcesVersion.setResourceName("客户端升级");
		resourcesVersion.setSaveType(SaveType.menu2.name());
		resourcesVersion.setSeq(2);
		resourcesVersion.setResourceUrl("/pro-client-version/");
		resourcesVersion.setUsingState("available");
		resourcesVersion.setParentId(resourcesAdmin.getPrimaryKey());
		sysResourcesRepository.save(resourcesVersion);

		SysResources resourcesUserList = new SysResources();
		resourcesUserList.setParentId(resourcesUser.getPrimaryKey());
		resourcesUserList.setAddTime(new Date());
		resourcesUserList.setIcon("icon-list-alt");
		resourcesUserList.setResourceName("用户列表");
		resourcesUserList.setSaveType(SaveType.menu2.name());
		resourcesUserList.setSeq(0);
		resourcesUserList.setResourceUrl("/pro-user/");
		resourcesUserList.setUsingState("available");
		sysResourcesRepository.save(resourcesUserList);

		SysResources resourcesExtractApply = new SysResources();
		resourcesExtractApply.setParentId(resourcesUser.getPrimaryKey());
		resourcesExtractApply.setAddTime(new Date());
		resourcesExtractApply.setIcon("icon-list-alt");
		resourcesExtractApply.setResourceName("提现申请");
		resourcesExtractApply.setSaveType(SaveType.menu2.name());
		resourcesExtractApply.setSeq(1);
		resourcesExtractApply.setResourceUrl("/pro-user-extract/apply/");
		resourcesExtractApply.setUsingState("available");
		sysResourcesRepository.save(resourcesExtractApply);

		SysResources resourcesExtractCash = new SysResources();
		resourcesExtractCash.setParentId(resourcesUser.getPrimaryKey());
		resourcesExtractCash.setAddTime(new Date());
		resourcesExtractCash.setIcon("icon-list-alt");
		resourcesExtractCash.setResourceName("提现记录");
		resourcesExtractCash.setSaveType(SaveType.menu2.name());
		resourcesExtractCash.setSeq(2);
		resourcesExtractCash.setResourceUrl("/pro-user-extract/cash/");
		resourcesExtractCash.setUsingState("available");
		sysResourcesRepository.save(resourcesExtractCash);

		SysResources resourcesOwner = new SysResources();
		resourcesOwner.setAddTime(new Date());
		resourcesOwner.setIcon("icon-list-alt");
		resourcesOwner.setResourceName("广告主用户中心");
		resourcesOwner.setSaveType(SaveType.menu1.name());
		resourcesOwner.setSeq(1);
		resourcesOwner.setUsingState("available");
		sysResourcesRepository.save(resourcesOwner);

		SysResources resourcesOwnerList = new SysResources();
		resourcesOwnerList.setParentId(resourcesOwner.getPrimaryKey());
		resourcesOwnerList.setAddTime(new Date());
		resourcesOwnerList.setIcon("icon-list-alt");
		resourcesOwnerList.setResourceName("用户列表");
		resourcesOwnerList.setSaveType(SaveType.menu2.name());
		resourcesOwnerList.setSeq(1);
		resourcesOwnerList.setResourceUrl("/pro-owner/");
		resourcesOwnerList.setUsingState("available");
		sysResourcesRepository.save(resourcesOwnerList);		

		SysResources resourcesAdvertOrder = new SysResources();
		resourcesAdvertOrder.setParentId(resourcesOwner.getPrimaryKey());
		resourcesAdvertOrder.setAddTime(new Date());
		resourcesAdvertOrder.setIcon("icon-list-alt");
		resourcesAdvertOrder.setResourceName("广告订单");
		resourcesAdvertOrder.setSaveType(SaveType.menu2.name());
		resourcesAdvertOrder.setSeq(2);
		resourcesAdvertOrder.setResourceUrl("/pro-owner/order/");
		resourcesAdvertOrder.setUsingState("available");
		sysResourcesRepository.save(resourcesAdvertOrder);
		
		SysResources resourcesDriverUser = new SysResources();
		resourcesDriverUser.setAddTime(new Date());
		resourcesDriverUser.setIcon("icon-list-alt");
		resourcesDriverUser.setResourceName("司机用户中心");
		resourcesDriverUser.setSaveType(SaveType.menu1.name());
		resourcesDriverUser.setSeq(2);
		resourcesDriverUser.setUsingState("available");
		sysResourcesRepository.save(resourcesDriverUser);

		SysResources resourcesDriverUserList = new SysResources();
		resourcesDriverUserList.setParentId(resourcesDriverUser.getPrimaryKey());
		resourcesDriverUserList.setAddTime(new Date());
		resourcesDriverUserList.setIcon("icon-list-alt");
		resourcesDriverUserList.setResourceName("用户列表");
		resourcesDriverUserList.setSaveType(SaveType.menu2.name());
		resourcesDriverUserList.setSeq(0);
		resourcesDriverUserList.setResourceUrl("/pro-driver-user/");
		resourcesDriverUserList.setUsingState("available");
		sysResourcesRepository.save(resourcesDriverUserList);

		SysResources resourcesDriverUserposition = new SysResources();
		resourcesDriverUserposition.setParentId(resourcesDriverUser.getPrimaryKey());
		resourcesDriverUserposition.setAddTime(new Date());
		resourcesDriverUserposition.setIcon("icon-list-alt");
		resourcesDriverUserposition.setResourceName("用户位置");
		resourcesDriverUserposition.setSaveType(SaveType.menu2.name());
		resourcesDriverUserposition.setSeq(1);
		resourcesDriverUserposition.setResourceUrl("/pro-driver-user/map/");
		resourcesDriverUserposition.setUsingState("available");
		sysResourcesRepository.save(resourcesDriverUserposition);

		SysResources resourcesDriverExtractApply = new SysResources();
		resourcesDriverExtractApply.setParentId(resourcesDriverUser.getPrimaryKey());
		resourcesDriverExtractApply.setAddTime(new Date());
		resourcesDriverExtractApply.setIcon("icon-list-alt");
		resourcesDriverExtractApply.setResourceName("提现申请");
		resourcesDriverExtractApply.setSaveType(SaveType.menu2.name());
		resourcesDriverExtractApply.setSeq(2);
		resourcesDriverExtractApply.setResourceUrl("/pro-driver-user-extract/apply/");
		resourcesDriverExtractApply.setUsingState("available");
		sysResourcesRepository.save(resourcesDriverExtractApply);

		SysResources resourcesDriverExtractCash = new SysResources();
		resourcesDriverExtractCash.setParentId(resourcesDriverUser.getPrimaryKey());
		resourcesDriverExtractCash.setAddTime(new Date());
		resourcesDriverExtractCash.setIcon("icon-list-alt");
		resourcesDriverExtractCash.setResourceName("提现记录");
		resourcesDriverExtractCash.setSaveType(SaveType.menu2.name());
		resourcesDriverExtractCash.setSeq(3);
		resourcesDriverExtractCash.setResourceUrl("/pro-driver-user-extract/cash/");
		resourcesDriverExtractCash.setUsingState("available");
		sysResourcesRepository.save(resourcesDriverExtractCash);

		SysResources resourcesMap = new SysResources();
		resourcesMap.setAddTime(new Date());
		resourcesMap.setIcon("icon-list-alt");
		resourcesMap.setResourceName("地图广告");
		resourcesMap.setSaveType(SaveType.menu1.name());
		resourcesMap.setSeq(3);
		resourcesMap.setUsingState("available");
		sysResourcesRepository.save(resourcesMap);

		SysResources resourcesMapListunderReview = new SysResources();
		resourcesMapListunderReview.setParentId(resourcesMap.getPrimaryKey());
		resourcesMapListunderReview.setAddTime(new Date());
		resourcesMapListunderReview.setIcon("icon-list-alt");
		resourcesMapListunderReview.setResourceName("审核列表");
		resourcesMapListunderReview.setSaveType(SaveType.menu2.name());
		resourcesMapListunderReview.setSeq(1);
		resourcesMapListunderReview.setResourceUrl("/pro-map-advertising/underReview/");
		resourcesMapListunderReview.setUsingState("available");
		sysResourcesRepository.save(resourcesMapListunderReview);

		SysResources resourcesMapList = new SysResources();
		resourcesMapList.setParentId(resourcesMap.getPrimaryKey());
		resourcesMapList.setAddTime(new Date());
		resourcesMapList.setIcon("icon-list-alt");
		resourcesMapList.setResourceName("广告列表");
		resourcesMapList.setSaveType(SaveType.menu2.name());
		resourcesMapList.setSeq(1);
		resourcesMapList.setResourceUrl("/pro-map-advertising/");
		resourcesMapList.setUsingState("available");
		sysResourcesRepository.save(resourcesMapList);
		
		SysResources resourcesWeb = new SysResources();
		resourcesWeb.setAddTime(new Date());
		resourcesWeb.setIcon("icon-list-alt");
		resourcesWeb.setResourceName("网页广告");
		resourcesWeb.setSaveType(SaveType.menu1.name());
		resourcesWeb.setSeq(3);
		resourcesWeb.setUsingState("available");
		sysResourcesRepository.save(resourcesWeb);

		SysResources resourcesWebListunderReview = new SysResources();
		resourcesWebListunderReview.setParentId(resourcesWeb.getPrimaryKey());
		resourcesWebListunderReview.setAddTime(new Date());
		resourcesWebListunderReview.setIcon("icon-list-alt");
		resourcesWebListunderReview.setResourceName("审核列表");
		resourcesWebListunderReview.setSaveType(SaveType.menu2.name());
		resourcesWebListunderReview.setSeq(1);
		resourcesWebListunderReview.setResourceUrl("/pro-web-advertising/underReview/");
		resourcesWebListunderReview.setUsingState("available");
		sysResourcesRepository.save(resourcesWebListunderReview);

		SysResources resourcesWebList = new SysResources();
		resourcesWebList.setParentId(resourcesWeb.getPrimaryKey());
		resourcesWebList.setAddTime(new Date());
		resourcesWebList.setIcon("icon-list-alt");
		resourcesWebList.setResourceName("广告列表");
		resourcesWebList.setSaveType(SaveType.menu2.name());
		resourcesWebList.setSeq(1);
		resourcesWebList.setResourceUrl("/pro-web-advertising/");
		resourcesWebList.setUsingState("available");
		sysResourcesRepository.save(resourcesWebList);

		SysResources resourcesWebCarrierList = new SysResources();
		resourcesWebCarrierList.setParentId(resourcesWeb.getPrimaryKey());
		resourcesWebCarrierList.setAddTime(new Date());
		resourcesWebCarrierList.setIcon("icon-list-alt");
		resourcesWebCarrierList.setResourceName("载体列表");
		resourcesWebCarrierList.setSaveType(SaveType.menu2.name());
		resourcesWebCarrierList.setSeq(2);
		resourcesWebCarrierList.setResourceUrl("/pro-web-advertising-carrier/");
		resourcesWebCarrierList.setUsingState("available");
		sysResourcesRepository.save(resourcesWebCarrierList);

		SysResources resourcesVideo = new SysResources();
		resourcesVideo.setAddTime(new Date());
		resourcesVideo.setIcon("icon-list-alt");
		resourcesVideo.setResourceName("视频广告");
		resourcesVideo.setSaveType(SaveType.menu1.name());
		resourcesVideo.setSeq(4);
		resourcesVideo.setUsingState("available");
		sysResourcesRepository.save(resourcesVideo);

		SysResources resourcesVideoList = new SysResources();
		resourcesVideoList.setParentId(resourcesVideo.getPrimaryKey());
		resourcesVideoList.setAddTime(new Date());
		resourcesVideoList.setIcon("icon-list-alt");
		resourcesVideoList.setResourceName("广告列表");
		resourcesVideoList.setSaveType(SaveType.menu2.name());
		resourcesVideoList.setSeq(0);
		resourcesVideoList.setResourceUrl("/pro-video-info/");
		resourcesVideoList.setUsingState("available");
		sysResourcesRepository.save(resourcesVideoList);

		// SysResources resourcesVideoTerminalList = new SysResources();
		// resourcesVideoTerminalList.setParentId(resourcesVideo.getPrimaryKey());
		// resourcesVideoTerminalList.setAddTime(new Date());
		// resourcesVideoTerminalList.setIcon("icon-list-alt");
		// resourcesVideoTerminalList.setResourceName("终端列表");
		// resourcesVideoTerminalList.setSaveType(SaveType.menu2.name());
		// resourcesVideoTerminalList.setSeq(1);
		// resourcesVideoTerminalList.setResourceUrl("/pro-video-reward/");
		// resourcesVideoTerminalList.setUsingState("available");
		// sysResourcesRepository.save(resourcesVideoTerminalList);

		/*********************** 超级管理员权限 *************************/
		SysRoleResources resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesVideo.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesWeb.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesDriverUser.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesUser.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesOwner.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesAdmin.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);
		
		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesMap.getPrimaryKey());
		resources.setRoleId(role_admin.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);
		
		

		/*********************** 全国代理权限 *************************/
		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesUser.getPrimaryKey());
		resources.setRoleId(role_national.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);
		
		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesOwner.getPrimaryKey());
		resources.setRoleId(role_national.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesDriverUser.getPrimaryKey());
		resources.setRoleId(role_national.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesVideo.getPrimaryKey());
		resources.setRoleId(role_national.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		/*********************** 上海代理权限 *************************/
		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesUser.getPrimaryKey());
		resources.setRoleId(role_shanghai.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesOwner.getPrimaryKey());
		resources.setRoleId(role_shanghai.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesDriverUser.getPrimaryKey());
		resources.setRoleId(role_shanghai.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesVideo.getPrimaryKey());
		resources.setRoleId(role_shanghai.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		/*********************** 兰州代理权限 *************************/
		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesUser.getPrimaryKey());
		resources.setRoleId(role_lanzhou.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesOwner.getPrimaryKey());
		resources.setRoleId(role_lanzhou.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesDriverUser.getPrimaryKey());
		resources.setRoleId(role_lanzhou.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		resources = new SysRoleResources();
		resources.setAddTime(new Date());
		resources.setResourcesId(resourcesVideo.getPrimaryKey());
		resources.setRoleId(role_lanzhou.getPrimaryKey());
		sysRoleResourcesRepository.save(resources);

		/*********************** 超級管理員赋权 *************************/
		SysAdminRole adminRole = new SysAdminRole();
		adminRole.setAddTime(new Date());
		adminRole.setAdminId(admin.getPrimaryKey());
		adminRole.setRoleId(role_admin.getPrimaryKey());
		sysAdminRoleRepository.save(adminRole);
	}
}
